//
//  TCSGTableView.m
//  iPing
//
//  Created by Thomas on 8/18/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TCSGTableView.h"


@implementation TCSGTableView
@dynamic PRS505DeviceExists;
/*-(NSMenu *)menuForEvent:(NSEvent *)theEvent {
    return theMenu;
}*/

-(NSMenu*)menuForEvent:(NSEvent*)event
{
	NSMenu * theSuperMenu = [[NSMenu alloc] init];
	
	[theSuperMenu addItemWithTitle:@"Show in Finder" action:@selector(showInFinder:) keyEquivalent:@""];
	[theSuperMenu addItemWithTitle:@"Convert to ePub" action:@selector(convertToePub:) keyEquivalent:@""];
	//[theSuperMenu addItem:superItem];
	//[[theSuperMenu itemAtIndex:2] setEnabled:YES];
	NSLog(@"helloMenu");
	//Find which row is under the cursor
	[[self window] makeFirstResponder:self];
	NSPoint menuPoint = [self convertPoint:[event locationInWindow] fromView:nil];
	int row = [self rowAtPoint:menuPoint];
	
	/* Update the table selection before showing menu
	 Preserves the selection if the row under the mouse is selected (to allow for
	 multiple items to be selected), otherwise selects the row under the mouse */
	BOOL currentRowIsSelected = [[self selectedRowIndexes] containsIndex:row];
	if (!currentRowIsSelected)
		[self selectRow:row byExtendingSelection:NO];
	
	if ([self numberOfSelectedRows] <=0)
	{
		NSLog(@"nothing selected");
        //No rows are selected, so the table should be displayed with all items disabled
		NSMenu* tableViewMenu = [theSuperMenu copy];
		int i;
		for (i=0;i<[tableViewMenu numberOfItems];i++)
			[[tableViewMenu itemAtIndex:i] setEnabled:NO];
		return [tableViewMenu autorelease];
	}
	else
	{
		NSLog(@"showing: %@",theSuperMenu);
		return theSuperMenu;
		
	}

	return theSuperMenu;
}
-(IBAction)showInFinder:(id)sender
{
	[[NSNotificationCenter defaultCenter] postNotificationName:MENUNOTIFICATION object:@"showInFinder"];
	//NSLog(@"selectedItems: %@",[self selectedItems]);
}
-(IBAction)convertToePub:(id)sender
{
	[[NSNotificationCenter defaultCenter] postNotificationName:MENUNOTIFICATION object:@"ePub"];
	//NSLog(@"selectedItems: %@",[self selectedItems]);
}
@end
